<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="厂区" prop="factoryId">
        <el-input
          v-model="queryParams.factoryId"
          placeholder="请输入厂区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="deptId">
        <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
</el-form-item>
      <el-form-item label="仓库编号" prop="storageId">
        <el-input
          v-model="queryParams.storageId"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="storageName">
        <el-input
          v-model="queryParams.storageName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库地址" prop="storageAddress">
        <el-input
          v-model="queryParams.storageAddress"
          placeholder="请输入仓库地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库联系人名称" prop="storageConcatsName">
        <el-input
          v-model="queryParams.storageConcatsName"
          placeholder="请输入仓库联系人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库联系人方式" prop="storageConcatsMobile">
        <el-input
          v-model="queryParams.storageConcatsMobile"
          placeholder="请输入仓库联系人方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否为立体库" prop="autoStatus">
        <el-select v-model="queryParams.autoStatus" placeholder="请选择是否为立体库" clearable>
          <el-option
            v-for="dict in dict.type.auto_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否是AGV库" prop="isAgv">
        <el-select v-model="queryParams.isAgv" placeholder="请选择是否是AGV库" clearable>
          <el-option
            v-for="dict in dict.type.is_agv"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓储类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择仓储类型" clearable>
          <el-option
            v-for="dict in dict.type.type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:storage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:storage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:storage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:storage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border
    ref="filterTable" v-loading="loading" :data="storageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed sortable label="主键" align="center" prop="id" />
      <el-table-column label="厂区" align="center" prop="factoryId" />
      <el-table-column label="所属部门" align="center" prop="deptId" />

      <el-table-column sortable label="仓库编号" align="center" prop="storageId" />
      <el-table-column label="仓库名称" align="center" prop="storageName" />
      <el-table-column label="仓库地址" align="center" prop="storageAddress" />
      <el-table-column label="仓库联系人名称" align="center" prop="storageConcatsName" />
      <el-table-column label="仓库联系人方式" align="center" prop="storageConcatsMobile" />
      <el-table-column label="是否为立体库" align="center" prop="autoStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.auto_status" :value="scope.row.autoStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否是AGV库" align="center" prop="isAgv">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_agv" :value="scope.row.isAgv"/>
        </template>
      </el-table-column>
      <el-table-column label="是否可用" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_use" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column label="仓储类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:storage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:storage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row >
          <el-col :span="12">
            <el-form-item label="厂区" prop="factoryId">
          <el-input v-model="form.factoryId" placeholder="请输入厂区" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请输入所属部门" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="仓库编号" prop="storageId">
          <el-input v-model="form.storageId" placeholder="请输入仓库编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="storageName">
          <el-input v-model="form.storageName" placeholder="请输入仓库名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="仓库地址" prop="storageAddress">
          <el-input v-model="form.storageAddress" placeholder="请输入仓库地址" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库联系人名称" prop="storageConcatsName">
          <el-input v-model="form.storageConcatsName" placeholder="请输入仓库联系人名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="仓库联系人方式" prop="storageConcatsMobile">
          <el-input v-model="form.storageConcatsMobile" placeholder="请输入仓库联系人方式" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否为立体库" prop="autoStatus">
          <el-select v-model="form.autoStatus" placeholder="请选择是否为立体库">
            <el-option
              v-for="dict in dict.type.auto_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="是否是AGV库" prop="isAgv">
          <el-select v-model="form.isAgv" placeholder="请选择是否是AGV库">
            <el-option
              v-for="dict in dict.type.is_agv"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓储类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择仓储类型">
            <el-option
              v-for="dict in dict.type.type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStorage, getStorage, delStorage, addStorage, updateStorage } from "@/api/system/storage";
import {deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";


export default {
  name: "Storage",
  dicts: ['is_agv', 'is_use', 'auto_status', 'type'],
  components: { Treeselect },
  data() {
    return {
      deptName: undefined,
      deptOptions: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      storageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryId: null,
        deptId: null,
        storageId: null,
        storageName: null,
        storageAddress: null,
        storageConcatsName: null,
        storageConcatsMobile: null,
        autoStatus: null,
        isAgv: null,
        status: null,
        type: null
      },
  
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryId: [
          { required: true, message: "厂区不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属部门不能为空", trigger: "blur" }
        ],
        storageId: [
          { required: true, message: "仓库编号不能为空", trigger: "blur" }
        ],
        storageName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
  },

  methods: {
      // 筛选节点
      filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listStorage(this.queryParams).then(response => {
        this.storageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryId: null,
        deptId: null,
        storageId: null,
        storageName: null,
        storageAddress: null,
        storageConcatsName: null,
        storageConcatsMobile: null,
        autoStatus: null,
        isAgv: null,
        status: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        type: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStorage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStorage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStorage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除仓库编号为"' + ids + '"的数据项？').then(function() {
        return delStorage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/storage/export', {
        ...this.queryParams
      }, `storage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
